package com.asas.spaceflight.controller;

import com.asas.spaceflight.model.Asteroid;
import com.asas.spaceflight.model.Asteroid.Type;
import com.asas.spaceflight.model.Bullet;
import com.asas.spaceflight.model.Player;
import com.asas.spaceflight.view.WorldRenderer;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;

public class ObjectFactory {

	public ObjectFactory() {
	}
	
	public static Bullet createBullet(Player player, float velocity, long lifetime) {
		
		Bullet obj = new Bullet();
		
		obj.setRadius(2);
		
		obj.getPosition().set(player.getHeight()/2, 0)
			.rotate(player.getAngle())
			.add(player.getPosition());

		obj.getVelocity().set(velocity, 0f)
			.rotate(player.getAngle());
		
		obj.setLifetime(lifetime);
		
		return obj;
	}
	
	public static void createStardust(Asteroid destroyed, Array<Asteroid> stardust) {
		
		switch (destroyed.getType()) {
		case BrideCake:
			for (int i = 0; i < 3; i++) {
				stardust.add(createAsteroid(Type.BrideCakePiece,
						destroyed.getPosition()));
			}			
			break;

		default:
			break;
		}
	}

	public static Asteroid createAsteroid(Type type, Vector2 pos) {

		Asteroid obj = new Asteroid(type);
		obj.getPosition().set(pos);
		obj.getVelocity().x = Random(-2, 2);
		obj.getVelocity().y = Random(-2, 2);
		
		switch (type) {
		case BrideCake:
			obj.setRadius(20);	
			obj.setHeight(obj.getRadius()*2);
			obj.setWidth(obj.getRadius()*2);
			break;
			
		case BrideCakePiece:
			obj.setRadius(Random(6, 10));	
			obj.setHeight(obj.getRadius()*2);
			obj.setWidth(obj.getRadius()*2);		
			break;

		default:
			break;
		}
		
		return obj;
	}

	// Dummy stuff
	public static Asteroid createDummyAsteroid() {

		Vector2 pos = new Vector2();
		Vector2 vel = new Vector2();
		float radius;

		float maxSpeed = 3;
		pos.x = Random(0, WorldRenderer.WIDTH);
		pos.y = Random(0, WorldRenderer.HEIGHT);
		vel.x = Random(-maxSpeed, maxSpeed);
		vel.y = Random(-maxSpeed, maxSpeed);
		radius = Random(8, 20);

		Asteroid obj = new Asteroid(Type.Dummy);
		obj.getPosition().set(pos);
		obj.getVelocity().set(vel);
		obj.setRadius(radius);
		obj.setHeight(radius*2);
		obj.setWidth(radius*2);
		
		return obj;
	}
	public static Asteroid createDummyBrideCake() {		
		Vector2 pos = new Vector2();
		pos.set(100, 100);
		return ObjectFactory.createAsteroid(Type.BrideCake, pos);		
	}
	
	static private float Random(float min, float max) {
		return (float)Math.random() * (max-min) + min;
	}

}
